
import { Logger } from "../utils/Logger";
import { Apple } from "../../Apple";
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const ws = require("ws");

let wsServerWorker; //wsServerWorker线程用来专门处理客户端请求连接

let arr = []

if (isMainThread) {
    arr = [1]
    Logger.log("[mainThread] arr", arr)
    Logger.log("[mainThread]", __filename)
    wsServerWorker = new Worker(__filename, {
        workerData: []
    })
    wsServerWorker.on("message", (param: Apple) => {
        Logger.log("[mainThread] connection", param)
        Logger.log("[mainThread] connection", param.id, param.name)
        wsServerWorker.postMessage(param);
    })

    wsServerWorker.stdout.on('data', (data) => {
        Logger.log("[wsServerWorker]", data + "")
    });
    wsServerWorker.stderr.on('data', (data) => {
        Logger.error("[wsServerWorker]", data + "")
    });
} else { //wsServerWorker线程
    console.log("wsServerWorker线程")
    const wsServer = new ws.Server({ port: 9696 });
    let connectNum = 0;
    wsServer.on('connection', function (connection) {
        // let socketConn = new SocketConnection(wsConnect);
        // SocketConnectionManager.instance.addSocketConnection(socketConn);
        connectNum++;
        console.log("connectNum====", connectNum, connection)
        let apple = new Apple(1, "testapple")
        parentPort.postMessage(connection);
        // parentPort.postMessage(apple);

    });
    parentPort.on('message', msg => {
        console.log("worker: receive ", msg);
    })

}


export class wsServer {


    // public static 
}
